package com.ourteam.product.api;

import java.util.Date;

import org.apache.commons.lang.BooleanUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ourteam.frame.controller.AbstractCRUDServiceController;
import com.ourteam.product.ui.dao.ProductUITableDAOQueryBean;
import com.ourteam.product.ui.domain.ProductUITableBean;
import com.ourteam.product.ui.domain.ProductUITableColumnBean;
import com.ourteam.product.ui.domain.ProductUITableColumnIconBean;
import com.ourteam.product.ui.service.IProductUIService;
import com.ourteam.product.ui.service.ProductUIServiceFactory;

import net.ui.model.table.ColumnBean;
import net.ui.model.table.ColumnDataBean;
import net.ui.model.table.TableBean;
import net.ui.model.table.TableConfig;

@Controller
@RequestMapping("/api/PRODUCT/UI/productUITable")
public class ProductUITableAPI extends
		AbstractCRUDServiceController<ProductUITableDAOQueryBean, ProductUITableSearchModel, ProductUITableBean> {

	private static final IProductUIService PRODUCT_UI_SERVICE = ProductUIServiceFactory.getProductUIService();

	@Override
	protected ProductUITableDAOQueryBean newQueryBean() {
		return new ProductUITableDAOQueryBean();
	}

	@Override
	protected int queryCount(ProductUITableDAOQueryBean queryBean) throws Exception {
		return PRODUCT_UI_SERVICE.queryProductUITableCount(queryBean);
	}

	@Override
	protected ProductUITableBean[] queryData(ProductUITableDAOQueryBean queryBean) throws Exception {
		return PRODUCT_UI_SERVICE.queryProductUITables(queryBean);
	}

	@Override
	protected void deleteData(long[] ids) throws Exception {
		PRODUCT_UI_SERVICE.deleteProductUITables(ids);
	}

	@Override
	protected ProductUITableBean getDataById(long id) throws Exception {
		return PRODUCT_UI_SERVICE.getProductUITableById(id);
	}

	@Override
	protected void saveData(ProductUITableBean dataBean) throws Exception {
		dataBean.setLastModifyTime(new Date());
		dataBean.setLastModifyUser(getLoginUserName());
		if (dataBean.getId() == 0) {
			PRODUCT_UI_SERVICE.addNewProductUITable(dataBean);
		} else {
			PRODUCT_UI_SERVICE.modifyProductUITable(dataBean);
		}

	}

	@RequestMapping("/getTable")
	@ResponseBody
	public TableBean getTable(@RequestParam long id) throws Exception {

		ProductUITableBean productUITableBean = PRODUCT_UI_SERVICE.getProductUITableById(id);

		TableConfig tableConfig = new TableConfig();

		TableBean tableBean = new TableBean();

		tableBean.setDesc(productUITableBean.getRemarks());

		tableBean.setId(String.valueOf(productUITableBean.getId()));

		//tableBean.setTableConfig(tableConfig);

		ProductUITableColumnBean[] tableColumnBeans = PRODUCT_UI_SERVICE
				.getProductUITableColumns(productUITableBean.getId());

		for (int j = 0; j < tableColumnBeans.length; j++) {
			ProductUITableColumnBean productUITableColumnBean = tableColumnBeans[j];

			ColumnBean columnBean = new ColumnBean();

			columnBean.setProperty(productUITableColumnBean.getCode());

			columnBean.setAlign(productUITableColumnBean.getAlign());

			columnBean.setCanFilter(BooleanUtils.toBoolean(productUITableColumnBean.getCanFilter()));

			columnBean.setCanReOrder(BooleanUtils.toBoolean(productUITableColumnBean.getCanReOrder()));

			columnBean.setCanResize(BooleanUtils.toBoolean(productUITableColumnBean.getCanResize()));

			columnBean.setCanSort(BooleanUtils.toBoolean(productUITableColumnBean.getCanSort()));

			columnBean.setCellEditor(productUITableColumnBean.getCellEditor());

			columnBean.setDataType(productUITableColumnBean.getDataType());

			columnBean.setIcon(productUITableColumnBean.getTitleIcon());

			columnBean.setLabel(productUITableColumnBean.getTitle());

			columnBean.setLabelKey(productUITableColumnBean.getTitleKey());

			columnBean.setListDataSource(productUITableColumnBean.getListDataSource());

			columnBean.setShowValue(BooleanUtils.toBoolean(productUITableColumnBean.getShowValue()));

			columnBean.setWidth(productUITableColumnBean.getWidth());

			if (StringUtils.isNotBlank(productUITableColumnBean.getFormatValue())) {

				ColumnDataBean columnDataBean = new ColumnDataBean();

				columnDataBean.setLabel("dateFormat");

				columnDataBean.setData(productUITableColumnBean.getFormatValue());

				columnBean.addColumnData(columnDataBean);

			}

			ProductUITableColumnIconBean[] iconBeans = PRODUCT_UI_SERVICE
					.getProductUITableColumnIcons(productUITableColumnBean.getId());

			for (int k = 0; k < iconBeans.length; k++) {

				ProductUITableColumnIconBean productUITableColumnIconBean = iconBeans[k];

				columnBean.addImage(productUITableColumnIconBean.getDataValue(),
						productUITableColumnIconBean.getDataIcon());

			}

			tableBean.addColumn(columnBean);
		}

		return tableBean;
	}

}
